package org.adnet.sdk;

import java.util.ArrayList;
import java.util.List;

import org.adnet.sdk.ad.SelfAd;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.telephony.TelephonyManager;

public class HttpUtils {

    private static String HOST = "http://121.199.45.209/offer.php";

    private static final String SDK_VERSION = "1.0";

    
    public static List<SelfAd> getAd(Context context) {
    	List<SelfAd> result = new ArrayList<SelfAd>();
    	try {
        	String imei = "unkown";
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null) {
            	imei = tm.getDeviceId();    
            }
            
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("a", "cpm"));
            params.add(new BasicNameValuePair("imei", imei));
            params.add(new BasicNameValuePair("sdk", SDK_VERSION));

            HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
            final HttpPost post = new HttpPost(HOST);
            post.setEntity(entity);

            HttpResponse httpResponse = getHttpClient().execute(post);

            
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                JSONObject responseJson = new JSONObject(EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
                if (responseJson.has("limit")) {

                	Utils.saveAdDuration(context, responseJson.getInt("limit") * 1000);
                	Utils.saveSelfAdWeight(context, responseJson.getInt("weight"));
                	
                	JSONArray list = responseJson.getJSONArray("list");
                	if (list.length() > 0) {
                		for (int i = 0; i < list.length(); i++) {
                    		JSONObject item = list.getJSONObject(0);
                    		
                    		SelfAd ad = new SelfAd();
                    		ad.setDownloadUrl(item.getString("url"));
                    		ad.setImage(item.getString("img"));
                    		ad.setOrder(item.getInt("order"));
                    		ad.setPackagename(item.has("package") ? item.getString("package") : "");
                    		result.add(ad);
                    	}	
                        if (result != null && result.size() > 0) {
                            
                            for (SelfAd info : result) {

                                if(CommonUtil.downloadIcon(MD5Checksum.getChecksum4String(info.getImage()) + ".png", info.getImage())) {
                                    info.setLocalimg(CommonUtil.ICON_PATH + MD5Checksum.getChecksum4String(info.getImage()) + ".png");
                                }
                            }
                        }
                	}
                	
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
    /**
     * Configures the httpClient to connect to the URL provided.
     */
    public static HttpClient getHttpClient() {
        HttpClient httpClient = new DefaultHttpClient();

        final HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 3000);
        HttpConnectionParams.setSoTimeout(params, 3000);

        return httpClient;
    }
}
